#!/usr/bin/env python
# coding=utf-8

from nlp.CoreNLPClient import  NERTag, Parse
import json
import sys
from celery import Celery, group
from utils.sexp import loads
from ParseTreePattern import Pattern1Apply, Pattern1Apply2
from DBUtils import GetSummaryByCve, GetCveList, storeResult, storeNodeResult

app = Celery('tasks')
app.config_from_object('celeryconfig')

@app.task
def NamedEntityExtract(text):
    result = NERTag(text.encode("utf-8"))
    sentence = result["sentences"][0]
    tokens = sentence["tokens"]

    phrases = []

    i = 0
    while i < len(tokens):
        tag = tokens[i]['ner']
        if tag not in ['O', 'NUMBER', 'MONEY', 'CVE']:
            words = []
            while i < len(tokens) and tokens[i]['ner'] == tag:
                words.append(tokens[i]['word'])
                i+=1
            phrases.append(words)
        else:
            i+=1
    return phrases, text


@app.task(trail=True, ignore_result=True)
def pattern1(cve, text):
    try:
        result = Pattern1Apply(text)
        storeResult(cve, text, result[0])
    except Exception, e:
        storeResult(text, str(e))


@app.task(trail=True, ignore_result=True)
def text2nodes(cve, text):
    try:
        result = Pattern1Apply2(text)
        storeResult(cve, text, "|||".join(result))
    except Exception, e:
        storeResult(cve, text, "ExceptionFound:{}".format(e))


